package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.InsertGeofenceDTO;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author liyuehui
 * @time 2024/7/11 9:44
 */
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 查询电子围栏列表
     * @param geofenceQuery 查询条件
     * @return List<GeofenceVO>
     */
    @Override
    public List<GeofenceVO> select(GeofenceQuery geofenceQuery) {
        return geofenceMapper.select(geofenceQuery);
    }


    /**
     * 更新电子围栏状态
     * @param id     电子围栏id
     * @param status 状态
     * @return int
     */
    @Override
    public int updateStatus(Long id, String status) {
        Geofence geofence = new Geofence();
        geofence.setId(id);
        geofence.setStatus(status);
        geofence.setUpdateTime(new Date());
        int result = geofenceMapper.update(geofence);
        if (result != 1) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return result;
    }

    /**
     * 保存电子围栏
     * @param insertGeofenceDTO 电子围栏信息
     * @return int
     */
    @Override
    public int save(InsertGeofenceDTO insertGeofenceDTO) {
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(insertGeofenceDTO, geofence);
        // 判断id是否为空, 如果为空, 则新增
        if (geofence.getId() == null) {
            //新增
            geofence.setCreateTime(new Date());
            int result = geofenceMapper.insert(geofence);
            if (result != 1) {
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
            return result;
        } else {
            // 更新
            geofence.setUpdateTime(new Date());
            int result = geofenceMapper.update(geofence);
            if (result != 1) {
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
            return result;
        }
    }

    /**
     * 删除电子围栏
     * @param geofenceId 电子围栏id
     * @return int
     */
    @Override
    public int delete(Long geofenceId) {
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(geofenceId);
        int count = vehicleMapper.selectVehicleCount(vehicleQuery);
        if (count > 0){
            throw new ServiceException(StatusCode.GEOFENCE_HAS_BIND_VEHICLE);
        }
        int result = geofenceMapper.delete(geofenceId);
        if (result != 1){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return result;
    }
}
